import pandas as pd
from openpyxl import Workbook
from openpyxl.utils.dataframe import dataframe_to_rows
from openpyxl.worksheet.dimensions import ColumnDimension

def create_sample_excel():
    # 创建一个包含示例IP地址的DataFrame
    ip_addresses = [f'192.168.1.{i}' for i in range(1, 255)]
    data = {
        '序号': list(range(1, 255)),
        'IP地址': ip_addresses,
        '描述': [''] * len(ip_addresses),
        '网络状态': ['未检测'] * len(ip_addresses),
        '备注': [''] * len(ip_addresses)
    }
    df = pd.DataFrame(data)
    
    # 将DataFrame保存为Excel文件
    file_path = 'ips.xlsx'
    
    # 使用openpyxl设置列宽
    wb = Workbook()
    ws = wb.active
    
    for r in dataframe_to_rows(df, index=False, header=True):
        ws.append(r)
    
    # 设置列宽
    ws.column_dimensions['A'].width = 10  # 序号
    ws.column_dimensions['B'].width = 15  # IP地址
    ws.column_dimensions['C'].width = 30  # 描述
    ws.column_dimensions['D'].width = 15  # 网络状态
    ws.column_dimensions['E'].width = 30  # 备注
    
    wb.save(file_path)
    print(f"Sample Excel file created: {file_path}")

if __name__ == "__main__":
    create_sample_excel()